package com.tjbank.cssys.web.config.MybatisSqlHandler;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.excel.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.rmi.transport.Connection;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

@Intercepts({@Signature(type = StatementHandler.class, method = "prepare",
        args = {Connection.class, Integer.class})})
public class AuditLogInterceptor implements Interceptor {

    private static Logger logger = LoggerFactory.getLogger(AuditLogInterceptor.class);

    /**
     * 包含操作人信息的注释模板
     */
    private static final String SQL_TEMPLATE = "/*@%s,%s@*/ %s";

    /**
     * 未知操作人
     */
    private static final String UNKNOWN_OPERATOR = "unknown";

    /**
     * 开关控制是否进行拦截 默认开启
     */
    private boolean enable = true;

    /**
     * 审计的表名称
     */
    private List<String> auditTables;

    /**
     * 拦截的数据库操作类型
     */
    private List<String> commandTypes = Arrays.asList(SqlCommandType.SELECT.toString());

    /**
     * SQL 类型
     */
    private String sqlType = JdbcConstants.MYSQL;


    public AuditLogInterceptor() {
        String interceptorEnabled = System.getProperty("auditlog.interceptor.enable");
        if (interceptorEnabled != null && !Boolean.valueOf(interceptorEnabled)) {
            this.enable = false;
        }
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        if (!enable) {
            return invocation.proceed();
        }
        // 判断是不是需要审计的命令类型
        if (!isAuditCommandType(invocation)) {
            return invocation.proceed();
        }

        // 判断是不是需要审计的表名
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        BoundSql boundSql = statementHandler.getBoundSql();
       /* if (!isAuditTable(invocation, boundSql.getSql())) {
            return invocation.proceed();
        }*/
        // 修改sql
        modifySql(boundSql);
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        String auditTables = (String) properties.get("auditTables");
        if (StringUtils.isBlank(auditTables)) {
            throw new IllegalArgumentException("Property auditTables is required");
        }

        this.auditTables = Arrays.stream(auditTables.toUpperCase().split(","))
                .map(String::trim)
                .collect(Collectors.toList());

        String sqlType = (String) properties.get("sqlType");
        if (StringUtils.isNotBlank(sqlType)) {
            this.sqlType = sqlType.toUpperCase();
        }

        String commandTypes = (String) properties.get("commandTypes");
        if (StringUtils.isNotBlank(commandTypes)) {
            this.commandTypes = Arrays.asList(commandTypes.toUpperCase().split(","));
        }
    }


    /**
     * 过滤不拦截的表
     *
     * @param invocation
     * @param sql
     * @return
     */
 /*   private boolean isAuditTable(Invocation invocation, String sql) {
        try {
            if (CollectionUtils.isEmpty(auditTables)) {
                return false;
            }

            String schema = getSchema(invocation);
            List<String> tables = getTables(schema, sql);
            for (String table : tables) {
                table = table.toUpperCase();
                for (String auditTable : auditTables) {
                    if (table.equals(auditTable) || table.matches(auditTable)) {
                        return true;
                    }
                }
            }
            logger.debug("filtered tables: {}", tables);
        } catch (Exception e) {
            logger.error("Failed check audit table", e);
            return false;
        }
        return false;
    }*/



    /**
     * 获取SQL命令类型
     *
     * @param invocation
     * @return
     */
    private boolean isAuditCommandType(Invocation invocation) {
        try {
            StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
            MetaObject metaObject =
                    MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY,
                            SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
            MappedStatement mappedStatement =
                    (MappedStatement) metaObject.getValue("delegate.mappedStatement");
            SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
            return sqlCommandType == null ? false : commandTypes.contains(sqlCommandType.toString());
        } catch (Exception e) {
            logger.error("Failed check sql command type", e);
            return false;
        }
    }


    /**
     * 在sql前面prepend注释信息，其包含操作人和traceId
     *
     * @param boundSql
     */
    private void modifySql(BoundSql boundSql) {
        try {
            // 修改sql
           /* Long customerId = CustomerIdGetterFactory.getCustomerId();
            String operatorId = customerId != null ? String.valueOf(customerId) : UNKNOWN_OPERATOR;
            String updatedSql = String.format(SQL_TEMPLATE, operatorId, TraceId.get(), boundSql.getSql());
            logger.debug("updatedSql: {}", updatedSql);

            // 回写sql
            Field field = boundSql.getClass().getDeclaredField("sql");
            field.setAccessible(true);
            field.set(boundSql, updatedSql);*/
        } catch (Exception e) {
            logger.error("Failed modify sql", e);
        }
    }

    /**
     * 解析数据库名称
     *
     * @param invocation
     * @return
     */
   /* private String getSchema(Invocation invocation) {
        Connection conn = (Connection) invocation.getArgs()[0];
        try {
           // return conn.getMetaData().getConnection().getCatalog();
        } catch (SQLException e) {
            logger.error("Failed get scheme", e);
            return null;
        }
    }

    *//**
     * 解析表名称
     *
     * @param schema
     * @param sql
     * @return
     *//*
    private List<String> getTables(String schema, String sql) {
        List<SQLStatement> statements = SQLUtils.parseStatements(sql, sqlType);
        return statements.stream().map(statement -> {
            MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
            statement.accept(visitor);
            String table = visitor.getCurrentTable();
            if (schema == null) {
                return table;
            }
            return table.contains(".") ? table : schema + "." + table;
        }).collect(Collectors.toList());
    }*/
}